package com.wwwarehouse.contract.program_operation.select_industry;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.contract.bean.ChooseModuleBean;
import com.wwwarehouse.contract.core.BaseViewPagerFragment;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.CheckIndusEvent;
import com.wwwarehouse.contract.event.IndusEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Creator by zhenwang.xiang on 2017/9/23 14:24
 */


public class SelectIndustryViewPagerFragment extends BaseViewPagerFragment implements View.OnClickListener {
	private List<ChooseModuleBean.ModuleList.BeanList> mCheckList = new ArrayList<>();
	private List<ChooseModuleBean.ModuleList.BeanList> mIndustryLastCheckedList = new ArrayList<>();
	private List<ChooseModuleBean.ModuleList.BeanList> mGangCaiDianDe = new ArrayList<>(); //刚刚瞎几把点的 又要取消的
	private boolean isFunction;
	private String mBusId;

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		EventBus.getDefault().register(this);
		if (getArguments() != null) {
			isFunction = getArguments().getBoolean(ContractConstant.KEY_IS_FUNCTION);
			mBusId = getArguments().getString(ContractConstant.KEY_BUNDLE_CONTRACT_BUID);
		}
		mCustomScroll.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return true;
			}
		});
		mConfirmBtn.setVisibility(View.VISIBLE);
		mConfirmBtn.setOnClickListener(this);
		mCheckList.clear();
	}

	@Override
	public void onResponse(String response, int what) {
		if (what == 0) {
			ChooseModuleBean chooseModuleBean = JSON.parseObject(response, ChooseModuleBean.class);
			if (chooseModuleBean != null && chooseModuleBean.getModuleList() != null && chooseModuleBean.getModuleList().getSize() > 0) {
				if (mIndustryLastCheckedList.size() > 0) {
					for (int i = 0; i < mIndustryLastCheckedList.size(); i++) {
						for (int j = 0; j < chooseModuleBean.getModuleList().getList().size(); j++) {
							if (mIndustryLastCheckedList.get(i).getCode().equals(chooseModuleBean.getModuleList().getList().get(j).getCode())) {
								chooseModuleBean.getModuleList().getList().get(j).setSelect(mIndustryLastCheckedList.get(i).isSelect());
							}
						}
					}
					mActivity.setTitle("选择所处行业(" + mIndustryLastCheckedList.size() + ")");
					if (mIndustryLastCheckedList.size() > 0) {
						mConfirmBtn.setEnabled(true);
						mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.blue_btn_circle));
					} else {
						mConfirmBtn.setEnabled(false);
						mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.btn_cir));
					}

				} else {
					if (isFunction) {
						mIndustryLastCheckedList.clear();
						for (int i = 0; i < chooseModuleBean.getModuleList().getList().size(); i++) {
							if (chooseModuleBean.getModuleList().getList().get(i).getSelected() == 1) {
								chooseModuleBean.getModuleList().getList().get(i).setSelect(true);
								mIndustryLastCheckedList.add(chooseModuleBean.getModuleList().getList().get(i));
							}
						}
						mActivity.setTitle("选择所处行业(" + mIndustryLastCheckedList.size() + ")");
						if (mIndustryLastCheckedList.size() > 0) {
							mConfirmBtn.setEnabled(true);
							mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.blue_btn_circle));
						} else {
							mConfirmBtn.setEnabled(false);
							mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.btn_cir));
						}
					}
				}
				setData(chooseModuleBean, (int) chooseModuleBean.getModuleList().getTotal(), 16, new SelectIndustryFragment().getClass().getName() , isFunction);
			}
		}

	}


	@Override
	public void setTitle() {
		if (peekFragment() instanceof SelectIndustryViewPagerFragment) {
			mActivity.setTitle(R.string.select_undustry);
		}
	}

	@Override
	public void requestDatas() {
		if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_BUNDLE_CHECKED_DATA) != null) {
			mIndustryLastCheckedList = (List<ChooseModuleBean.ModuleList.BeanList>) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_CHECKED_DATA);
		}

		Map<String, Object> map = new HashMap<>();
		map.put("page", 1);
		map.put("size", 10000);
		Map<String, Object> param = new HashMap<>();
		param.put("baseQuery", map);
//        param.put("buId", 260001);
		param.put("sourceList", new ArrayList<>());
		param.put("sourceModule", "INDUSTRY");
		if (isFunction) {
			param.put("taskType", "function");
			param.put("buId", mBusId);
		} else {
			param.put("taskType", "task");
		}
		sendRequest(ContractConstant.PLANNING_CHOOSE_MODULE, param, 0);


	}

	public void onEventMainThread(IndusEvent event) {

		//step1  只添加手动选中的条目
		if (!mCheckList.contains(event.getList()) && event.getList().isSelect()) {
			mCheckList.add(event.getList());
		} else if (mCheckList.contains(event.getList()) && !event.getList().isSelect()){
			mCheckList.remove(event.getList());
		}

		//step 2 从两个List中删选， 如果把默认选中的条目手动取消了，则去默认选中的容器中删除，保证两个容器条目不重复。
		for (int i = 0; i < mIndustryLastCheckedList.size(); i++) {
			if (!event.getList().isSelect() && event.getList().getCode().equals(mIndustryLastCheckedList.get(i).getCode())) {
				mGangCaiDianDe.add(mIndustryLastCheckedList.get(i));
				mIndustryLastCheckedList.remove(i);
			}

		}
		//step 3 合并两个容器返回上层
		List<ChooseModuleBean.ModuleList.BeanList> finalList = new ArrayList<>();
		finalList.addAll(mCheckList);
		finalList.addAll(mIndustryLastCheckedList);

		//
		if (finalList.size() > 0) {
			mActivity.setTitle("选择所处行业(" + finalList.size() + ")");
			mConfirmBtn.setEnabled(true);
			mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.blue_btn_circle));
		} else {
			mActivity.setTitle("选择所处行业");
			mConfirmBtn.setEnabled(false);
			mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.btn_cir));
		}
	}



	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}


	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.confirm_btn) {
				List<ChooseModuleBean.ModuleList.BeanList> finalList = filterData();
				EventBus.getDefault().post(new CheckIndusEvent(finalList));
			popFragment();
		}
	}

	@NonNull
	private List<ChooseModuleBean.ModuleList.BeanList> filterData() {
		List<ChooseModuleBean.ModuleList.BeanList> finalList = new ArrayList<>();
		finalList.addAll(mCheckList);
		finalList.addAll(mIndustryLastCheckedList);
		return finalList;
	}

	public void onEventMainThread(BackListenerEvent backListenerEvent) {
		if (backListenerEvent.getMsg().equals("SelectIndustryViewPagerFragment")) {
			mIndustryLastCheckedList.addAll(mGangCaiDianDe);
			EventBus.getDefault().post(new CheckIndusEvent(mIndustryLastCheckedList));
			popFragment();
		}
	}
}
